package org.atovio.quickweb.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.*;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 使用验证功能
 *
 * @author AtoVio, Winter Yuan
 */
@Data
public class Article {

    // 分组验证，用接口来实现组的功能
    public interface AddArticleGroup {
    }
    public interface EditArticleGroup {
    }

    @NotNull(message = "必须指定文章id", groups = EditArticleGroup.class)
    @Min(value = 1, message = "id不能小于1", groups = {EditArticleGroup.class, AddArticleGroup.class})
    private Integer id;

    @NotNull(message = "用户id不能为空", groups = {AddArticleGroup.class, EditArticleGroup.class})
    @Min(value = 1, message = "id不能小于1", groups = {EditArticleGroup.class, AddArticleGroup.class})
    private Integer userId;

    @NotBlank(message = "题目不能为空", groups = {AddArticleGroup.class, EditArticleGroup.class})
    // @Size认为null有效
    @Size(min = 3, max = 30, message = "标题字数在3~30之间", groups = {AddArticleGroup.class, EditArticleGroup.class})
    private String title;

    // 注意！！！LocalDateTime是不能被解析的。我们需要使用这个注解设置日期格式
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Past(message = "必须是过去的时间", groups = {AddArticleGroup.class, EditArticleGroup.class})
    private LocalDateTime createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Past(message = "必须是过去的时间", groups = {AddArticleGroup.class, EditArticleGroup.class})
    private LocalDateTime updateTime;

    @Email(message = "不符合邮箱格式", groups = {AddArticleGroup.class, EditArticleGroup.class})
    private String email;
}
